1.Create a Notebook to demonstrate NumPy Operations.
import numpy as np
import plotly
plotly.offline.init_notebook_mode()
import plotly.express as px
2.Create an array that starts from the integer 1, ends at 20, incremented by 3.
arr_1 = np.arange(1,20,3)
arr_1
array([ 1, 4, 7, 10, 13, 16, 19])
3.Create a new array of shape 3 with random numbers between 0 and 1.
arr_2 = np.random.rand(1,3)
print('arr_2:',arr_2)
arr_3 = np.random.rand(3,3)
print('arr_3:',arr_3)
arr_2: [[0.49128196 0.52724615 0.03805312]] arr_3: [[0.39700169 0.27507444 0.02112062] [0.01568394 0.84747021 0.20514261] [0.92916745 0.38634494 0.6338459 ]]
4.Create a 2 D array [[10,20,45], [30,12,16], [42,17,56]] and perform the following operations:
Slice the 2D array to get the first two rows, slice the 2D array to get the last two rows.
arr_5 = np.array([[10,20,45], [30,12,16], [42,17,56]])
print(arr_5[0:2])
print(arr_5[len(arr_5):0:-1])
[[10 20 45] [30 12 16]] [[42 17 56] [30 12 16]]
5.Create two 2x2 arrays and demonstrate how you can stack the elements vertically, horizontally, and split the arrays into smaller arrays.
arr_6_1 = [[1,2],[3,4]]
arr_6_2 = [[5,6],[7,8]]
print(arr_6_1)
print(arr_6_2)
# vertically
ver_stack = np.vstack((arr_6_1,arr_6_2))
print(ver_stack)
# vertical split
print(np.vsplit(ver_stack,2))
# horizontally
hor_stack = np.hstack((arr_6_1, arr_6_2))
print(hor_stack)
# horizontal split
print(np.hsplit(hor_stack,2))
# split the array into smaller arrays
combined_array = np.concatenate((arr_6_1,arr_6_2),axis=None)
print(np.split(combined_array,combined_array.size))
[[1, 2], [3, 4]]
[[5, 6], [7, 8]]
[[1 2]
[3 4]
[5 6]
[7 8]]
[array([[1, 2],
[3, 4]]), array([[5, 6],
[7, 8]])]
[[1 2 5 6]
[3 4 7 8]]
[array([[1, 2],
[3, 4]]), array([[5, 6],
[7, 8]])]
[array([1]), array([2]), array([3]), array([4]), array([5]), array([6]), array([7]), array([8])]
6.Create two matrices X= ([[5, 7, 2], [4, 5, 6], [7, 4 ,2]]) Y= ([[4, 2], [6, 2], [4, 2]]), Is it possible to multiply these matrices?
X= ([[5, 7, 2], [4, 5, 6], [7, 4 ,2]])
Y= ([[4, 2], [6, 2], [4, 2]])
# Demonstrate the case when it is not possible to.
# np.multiply(X,Y) it's not possible due to elements not being of same wise
np.matmul(X,Y)
array([[70, 28],
[70, 30],
[60, 26]])
7.Create two arrays, x = ([2, -1, -8]) y = ([3, 1, -2]), Find the Shape, Number of dimensions of vector x.
Reshape the vector x to a matrix of size (3,1) and determine the number of dimensions after reshaping y to a matrix of (3,1)
x = ([2, -1, -8])
y = ([3, 1, -2])
x_np = np.array(x)
y_np = np.array(y)
#shape
print('shape of x and y')
print('x:', x_np.shape)
print('y:', y_np.shape)
# Number of dimensions
print('number of dimensions of x and y')
print('x:', x_np.ndim)
print('y:', y_np.ndim)
# reshape of x to (3,1)
print('reshape of x to (3,1)')
print('x:', x_np.reshape((3,1)))
# reshape of y to (3,1) and get dimensions
print('reshape of y to (3,1) and finding dimensions')
print('y:', y_np.reshape((3,1)).ndim)
shape of x and y x: (3,) y: (3,) number of dimensions of x and y x: 1 y: 1 reshape of x to (3,1) x: [[ 2] [-1] [-8]] reshape of y to (3,1) and finding dimensions y: 2
boardcasting in numpy means how it treats the matrix differently during arithmetic operations
arr_7 = np.array([[1,2,3],[4,5,6],[7,8,9]])
arr_8 = np.array([[10,11,12],[13,14,15],[16,17,18]])
arr_9 = np.array([[10,11,12]])
arr_10 = np.array([[10,11,12, 14]])
print(arr_7)
print(arr_8)
# subtraction
print('-------')
print('subtraction will done by row wise by elements like 10 - 1, 11 - 2, 12 - 3, 13 - 4 ... so on')
print('-------')
print(arr_8 - arr_7)
print('-------')
print('if X = 3 * 3 and Y = 3 * 1 subtraction is done then it will subtract from Y single row with X all rows.')
print('-------')
print(arr_9 - arr_7)
print('-------')
print("it's impossible to subtract if row size is not same for X and Y")
print("print(arr_10 - arr_7) error for this: operands could not be broadcast together with shapes (1,4) (3,3)")
# multiplication
print('-------')
print("Multiplication will done by row wise by elements like 10 * 1, 11 * 2, 12 * 3, 13 * 4 ... so on")
print('-------')
print(arr_8 * arr_7)
print('-------')
print("if X = 3 X 3 and Y = 3 * 1 subtraction is done then it will subtract from Y single row with X all rows.")
print('-------')
print(arr_9 * arr_7)
print('-------')
print("it's impossible to multiple if row size is not same for X and Y")
print("print(arr_7 * arr_10) error for this: operands could not be broadcast together with shapes (3,3) (1,4)")
[[1 2 3] [4 5 6] [7 8 9]] [[10 11 12] [13 14 15] [16 17 18]] ------- subtraction will done by row wise by elements like 10 - 1, 11 - 2, 12 - 3, 13 - 4 ... so on ------- [[9 9 9] [9 9 9] [9 9 9]] ------- if X = 3 * 3 and Y = 3 * 1 subtraction is done then it will subtract from Y single row with X all rows. ------- [[9 9 9] [6 6 6] [3 3 3]] ------- it's impossible to subtract if row size is not same for X and Y print(arr_10 - arr_7) error for this: operands could not be broadcast together with shapes (1,4) (3,3) ------- Multiplication will done by row wise by elements like 10 * 1, 11 * 2, 12 * 3, 13 * 4 ... so on ------- [[ 10 22 36] [ 52 70 90] [112 136 162]] ------- if X = 3 X 3 and Y = 3 * 1 subtraction is done then it will subtract from Y single row with X all rows. ------- [[ 10 22 36] [ 40 55 72] [ 70 88 108]] ------- it's impossible to multiple if row size is not same for X and Y print(arr_7 * arr_10) error for this: operands could not be broadcast together with shapes (3,3) (1,4)
Feel free to add additional Markdown elements (description/additional comments)
A graph representing categorical data with rectangular bars of varying heights.
fig = px.bar(x=["a", "b", "c"], y=[1, 3, 2])
fig.show()
A graph displaying individual data points as markers on a coordinate grid.
data = px.data.iris()
fig = px.scatter(data, x="sepal_width", y="sepal_length", color="species")
fig.show()
A graph showing the trend of a variable over a continuous range of values.
df = px.data.gapminder().query("continent=='Oceania'")
fig = px.line(df, x="year", y="lifeExp", color="country", title='Life Expectancy in Oceania')
fig.show()
A circular chart divided into sectors, representing the proportion of different categories in a dataset.
df = px.data.tips()
fig = px.pie(df, values='tip', names='day')
fig.show()
1.Solve the following two system of linear equations using matrices (find the values of x1 and x2) and determine the number of solutions using the functions.
To find the values of x1, x2, and x3, you need a system of linear equations with the same number of equations as variables. In this case, you have two equations (2x1 + 3x2 - 4x3 = 6 and x1 - 4x2 = 8) and three variables (x1, x2, and x3).
To solve for x1, x2, and x3, we need a third equation. Without any further constraints, there are infinitely many solutions possible.
For instance, let's consider one possible solution:
x1 = 1
x2 = 2
x3 = 3
By substituting these values into the equations, we get:
2(1) + 3(2) - 4(3) = 6
2 + 6 - 12 = 6
8 - 12 = 6
-4 = 6 (not true)
(1) - 4(2) = 8
1 - 8 = 8
-7 = 8 (not true)
As you can see, the chosen values of x1 = 1, x2 = 2, and x3 = 3 do not satisfy the given equations. However, there are other combinations of x1, x2, and x3 that would satisfy the equations.
To find a unique solution, we would need additional equations or constraints that relate the variables or provide specific values for some of the variables. Without such information, the system remains underdetermined, and we cannot determine unique numerical values for x1, x2, and x3.
So from given equations, let us make the first one into two equations for solving two variables called x1 and x2.
First values are
The X matrix will be
| X1 | X2 |
|---|---|
| 2 | 3 |
| 1 | -4 |
The Y matrix will be
| Y |
|---|
| 6 |
| 8 |
X = np.array([[2, 3], [1, -4]])
Y = np.array([6,8])
print('The Solution is', np.linalg.solve(X,Y))
The Solution is [ 4.36363636 -0.90909091]
Let us make the second one into third equation, where we are creating new equation to solve it.
2. 3y1-4y2+5y3=10,
-y1+2y2-4y3=8
for making the third equation we will add second equation y1 value into first equation
-y1+2y2-4y3=8
y1 = -8 + 2y2 -4y3
adding y1 into first equation
3y1-4y2+5y3=10
3(-8 + 2y2 -4y3)-4y2+5y3 = 10
2y2 - 7y3 = 14
where y1 is 0
Second values are
The X matrix will be
| X1 | X2 | X3 |
|---|---|---|
| 3 | -4 | 5 |
| -1 | 2 | 4 |
| 0 | 2 | -7 |
The Y matrix will be
| Y |
|---|
| 10 |
| 8 |
| 14 |
The markdown table is not visible when convert to html | Y | | ---- | | 10 | | 8 | | 14|
X = np.array([[3, -4, 5], [-1, 2, 4],[0, 2, -7]])
Y = np.array([10,8, 14])
print('The Solution is',np.linalg.solve(X,Y))
The Solution is [15.16666667 9.91666667 0.83333333]